package com.example.easypoi.service.impl;

import com.example.easypoi.mapper.DistrictMapper;
import com.example.easypoi.model.District;
import com.example.easypoi.service.DistrictService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: 朱超
 * @createDate: 2023/7/6
 * @version: 1.0
 * @remark：备注
 */
@Service
public class DistrictServiceImpl implements DistrictService {

    @Resource
    private DistrictMapper districtMapper;

    @Override
    public Integer AddBankTest(List<District> bankList) {
        return districtMapper.AddBankTest(bankList);
    }

    @Override
    public List<District> queryList() {
        long start = System.currentTimeMillis();
        //一级
        List<District> districts = districtMapper.queryList();
        List<District> list = new ArrayList<>();
        districts.forEach(s -> {
            District district = new District();
            BeanUtils.copyProperties(s, district);
            list.add(district);
            district.setChildren(districts);
            //二级
            List<District> list1 = new ArrayList<>();
            List<District> lis = districtMapper.queryListTwo(s.getValue());
            lis.forEach(s1 -> {
                District district1 = new District();
                BeanUtils.copyProperties(s1, district1);
                list1.add(district1);
                district.setChildren(list1);
                //三级
                List<District> list2 = new ArrayList<>();
                List<District> districtList = districtMapper.queryListThtee(s1.getValue());
                districtList.forEach(s2 -> {
                    District distr = new District();
                    distr.setCode(s2.getCode());
                    distr.setCity(s2.getCity());
                    distr.setLabel(s2.getLabel());
                    distr.setValue(s2.getValue());
                    list2.add(distr);
                    district1.setChildren(list2);
                });
            });
        });
        long end = System.currentTimeMillis();
        System.out.println("耗时毫秒数:" + (end - start));
        return list;
    }

    /**
     * 改用map
     *
     * @return
     */
    @Override
    public List<District> queryList11() {
        long start = System.currentTimeMillis();
        // 所有的省市区都在里面
        List<District> regionList = districtMapper.findAll();
        // 由于只需要给 省 和 市 添加儿子
        // 所以我们将所有的 省和市  作为value  省code和市code作为key  放入map中
        Map<String, District> provinceMap = new HashMap<>();
        Map<String, District> cityMap = new HashMap<>();
        for (District region : regionList) {
            if (region.getLevel() == 1) {
                // 当前region是省 放入省map
                provinceMap.put(region.getValue().toString(), region);
            } else if (region.getLevel() == 2) {
                // 当前region是市 放入市map
                cityMap.put(region.getValue().toString(), region);
            }
        }
        List<District> provinceList = new ArrayList<>();
        for (District region : regionList) {
            if (region.getLevel() == 2) {
                // 这是市 获取到市的 父code,然后从 省map中获取到省对象,然后将当前 市放入省的children
                String provinceCode = region.getCode().toString(); // 当前市的 父级 也就是省的 code
                // 获取当前市的 父级别 省的对象
                District province = provinceMap.get(provinceCode);
                // 将市对象 添加到 省对象的 儿子中
                province.getChildren().add(region);
            } else if (region.getLevel() == 3) {
                String cityCode = region.getCode().toString();
                District city = cityMap.get(cityCode);
                city.getChildren().add(region);
            } else {
                provinceList.add(region);
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("耗时毫秒数:" + (end - start));
        return provinceList;
    }

}
